package com.ziyun.job.modules.job.task.order;

import com.ziyun.erp.modules.member.entity.StoreMemberEntity;
import com.ziyun.erp.modules.member.entity.UserBaseInfoEntity;
import com.ziyun.erp.modules.member.service.StoreMemberService;
import com.ziyun.erp.modules.member.service.UserBaseInfoService;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import io.renren.modules.sys.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jiangli
 * @date 2018-12-22 16:26
 * 小B紫云网新增用户同步
 */
@Component("memberSyncTwb")
public class MemberSyncTwb {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(MemberSyncTwb.class);

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Autowired
    private StoreMemberService storeMemberService;
    @Autowired
    private MailService mailService;

    public void memberSyncTwb() {
        logger.info("========== 开始小B紫云网新增用户同步 ==========");
        //查询未同步的订单
        List<Map<String, Object>> syncTwbList = orderInfoService.memberSyncTwb();
        List<StoreMemberEntity> storeMemberEntityList=new ArrayList<>();
        for (Map<String, Object> map : syncTwbList) {
            StoreMemberEntity storeMemberEntity = new StoreMemberEntity();
            storeMemberEntity.setMemberId((Integer) map.get("user_id"));
            storeMemberEntity.setSellerId((Integer) map.get("supplier_id"));
            storeMemberEntity.setMerchantsType("visit");
            storeMemberEntity.setRemark("小B紫云网新增用户同步");
            //线上客户来源商城
            storeMemberEntity.setMerchantsSource(0);
            //查询用户信息
            UserBaseInfoEntity userBaseInfo = userBaseInfoService.selectById((Integer) map.get("user_id"));
            if (userBaseInfo != null) {
                storeMemberEntity.setAddr(userBaseInfo.getAddress());
                storeMemberEntity.setMobile(userBaseInfo.getMobile());
                storeMemberEntity.setTel(userBaseInfo.getTel());
                storeMemberEntity.setName(userBaseInfo.getName());
            }
            storeMemberEntityList.add(storeMemberEntity);
        }
        //向store_member插入数据
        if (storeMemberEntityList.size()>0) {
            storeMemberService.insertBatch(storeMemberEntityList);
        }

        logger.info("========== 小B紫云网新增用户同步完毕 ==========");
     }

}
